package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.base.domain.BaseFruitOrderDetail;
import com.ruoyi.base.mapper.BaseFruitOrderDetailMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseFruitOrderMapper;
import com.ruoyi.base.domain.BaseFruitOrder;
import com.ruoyi.base.service.IBaseFruitOrderService;

/**
 * 订单操作Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-23
 */
@Service
public class BaseFruitOrderServiceImpl implements IBaseFruitOrderService {
    @Autowired
    private BaseFruitOrderMapper baseFruitOrderMapper;

    @Autowired
    private BaseFruitOrderDetailMapper baseFruitOrderDetailMapper;

    /**
     * 查询订单操作
     *
     * @param id 订单操作主键
     * @return 订单操作
     */
    @Override
    public BaseFruitOrder selectBaseFruitOrderById(Long id) {
        return baseFruitOrderMapper.selectBaseFruitOrderById(id);
    }

    /**
     * 查询订单操作列表
     *
     * @param baseFruitOrder 订单操作
     * @return 订单操作
     */
    @Override
    public List<BaseFruitOrder> selectBaseFruitOrderList(BaseFruitOrder baseFruitOrder) {
        List<BaseFruitOrder> baseFruitOrders = baseFruitOrderMapper.selectBaseFruitOrderList(baseFruitOrder);

        for (BaseFruitOrder fruitOrder : baseFruitOrders) {
            List<BaseFruitOrderDetail> orderDetailList = baseFruitOrderDetailMapper.getOrderDetailList(fruitOrder.getId());

            fruitOrder.setOrderItemList(orderDetailList);
        }

        return baseFruitOrders;
    }

    /**
     * 新增订单操作
     *
     * @param baseFruitOrder 订单操作
     * @return 结果
     */
    @Override
    public int insertBaseFruitOrder(BaseFruitOrder baseFruitOrder) {
        baseFruitOrder.setCreateTime(DateUtils.getNowDate());
        baseFruitOrder.setId(System.currentTimeMillis());
        List<BaseFruitOrderDetail> orderItemList = baseFruitOrder.getOrderItemList();

        for (BaseFruitOrderDetail item : orderItemList) {
            item.setOrderId(baseFruitOrder.getId());
            baseFruitOrderDetailMapper.insertBaseFruitOrderDetail(item);
        }
        return baseFruitOrderMapper.insertBaseFruitOrder(baseFruitOrder);
    }

    /**
     * 修改订单操作
     *
     * @param baseFruitOrder 订单操作
     * @return 结果
     */
    @Override
    public int updateBaseFruitOrder(BaseFruitOrder baseFruitOrder) {
        return baseFruitOrderMapper.updateBaseFruitOrder(baseFruitOrder);
    }

    /**
     * 批量删除订单操作
     *
     * @param ids 需要删除的订单操作主键
     * @return 结果
     */
    @Override
    public int deleteBaseFruitOrderByIds(Long[] ids) {
        return baseFruitOrderMapper.deleteBaseFruitOrderByIds(ids);
    }

    /**
     * 删除订单操作信息
     *
     * @param id 订单操作主键
     * @return 结果
     */
    @Override
    public int deleteBaseFruitOrderById(Long id) {
        return baseFruitOrderMapper.deleteBaseFruitOrderById(id);
    }
}
